﻿@using MvcContrib.UI.Grid
@model XRM.Security.WebUI.Models.WorkKind.WorkKindModel
@{
    ViewBag.Title = String.Format("{0}", Model.IsEdit ? Model.Name : "Добавление видов работ");
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@Html.Partial("ReturnLinkPartial", new Tuple<string, string>(Url.Action("Index"), "Вернуться к списку видов работ"))
<div>
    @using (Html.BeginForm("Edit", "WorkKind", FormMethod.Post, new { @class = "form-horizontal form-horizontal-2" }))
    {
        @Html.AntiForgeryToken()

        const string inputCss = "form-control";
        const string divCss = "col-sm-4";

        @Html.HiddenFor(m => m.Id)
        @Html.HiddenFor(m => m.IsFixed)

        if (Model.IsFixed)
        {
            @Html.HiddenFor(m => m.IsLinkedWithSign)
        }
        
            <div class="form-group">
                @Html.LabelFor(m => m.Name, new { @class = "control-label required" })
                <div class="@divCss">
                    @Html.TextBoxFor(m => m.Name, new { @class = @inputCss })
                    @Html.ValidationMessageFor(m => m.Name)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(m => m.Description, new { @class = "control-label" })
                <div class="@divCss">
                    @Html.TextBoxFor(m => m.Description, new { @class = @inputCss })
                    @Html.ValidationMessageFor(m => m.Description)
                </div>
            </div>
            <div class="form-group">
                <label class = "control-label">&nbsp;</label>
                <div class="checkbox @divCss">
                    <label for="IsLinkedWithSign">
                        @if (!Model.IsFixed)
                        {
                            @Html.CheckBoxFor(x => x.IsLinkedWithSign)
                        }
                        else
                        {
                            @Html.CheckBox("IsLinkedWithSign", Model.IsLinkedWithSign, new { disabled = "disabled" } )
                        }
                        Связан с ЭП
                    </label>
                </div>    
            </div>
          
            <div class="form-group">
                @Html.LabelFor(m => m.ParamList, new { @class = "control-label" })
                <div class="@divCss">
                    <div data-bind="template: { name: 'param-template', foreach: Param }"></div>
                    @if ((!Model.ReadOnly) && (Model.AllowParamCRUD))
                    {
                        <div class="control-text">
                            <a href="#" data-bind="click: addParam">Добавить параметр</a>
                        </div>
                    }
                    @Html.ValidationMessageFor(m => m.ParamList)
                </div>
            </div>
            
                    if (!Model.ReadOnly)
                    {
                @Html.Partial("_FormActionsPartial")
                    }

    }
</div>

<script type="text/html" id="param-template">
    <div class="input-list-item" data-bind="css: { 'input-group': $index() >= 0 }">
        <input type="hidden" data-bind="attr: { name: 'ParamList[' + $index() + '].Id', value: id }" />
        <select class="form-control" data-bind="attr: { name: 'ParamList[' + $index() + '].ddl', onchange: 'SetParamId(' + $index() + ');' }, options: ParamAll, optionsText: 'name', optionsValue: 'id', value: selectedValue, disable: (disabled == 1)"  />
        @if (!Model.ReadOnly)
        {
        <span class="input-group-btn" data-bind="">
            <button title="Удалить параметр" class="btn btn-default" type="button" data-bind="click: $parent.removeParam, disable: (disabled == 1)">
                <i class="fa fa-times"></i>
            </button>
        </span>
        }
    </div>
</script>

@section scripts
{
     @Scripts.Render("~/bundles/gridlibrary")

    <script type="text/javascript" src="@(Url.Content("~/Scripts/FileUploader.js"))"></script>
    <script type="text/javascript">
        $(document).ready(function () {
        });

        function SetParamId(index) {
            nameInput = 'ParamList[' + index + ']';
            $('input[name="' + nameInput + '.Id"]').val($('select[name="' + nameInput + '.ddl"]').val());
        }
    </script>
    <script src="@Url.Content("~/Scripts/knockout-3.2.0.js")"></script>
    <script type="text/javascript">
        function WorkKindViewModel() {
            var self = this;

            self.ParamAll = ko.observableArray([
                @{
                    if ((Model.ParamAllList != null) && (Model.ParamAllList.Any()))
                    {
                        foreach (var item in Model.ParamAllList)
                        {
                            <text>{ id: '@item.Id', name: '@item.Name.Replace("\\", "\\\\")' },</text>
                        }
                    }
                    else
                    {
                        <text>{ id: '-1', name: '' }</text>
                    }
                }
            ]);

            self.Param = ko.observableArray([
                @{
                    if ((Model.ParamList != null) && (Model.ParamList.Any()))
                    {
                        foreach (var item in Model.ParamList)
                        {
                            <text>{ id: '@item.Id', name: '@item.Name.Replace("\\", "\\\\")', ParamAll: self.ParamAll, selectedValue: '@item.Id', disabled: '@(Model.AllowParamCRUD ? "0" : "1")' },</text>
                        }
                    }
                    else
                    {
                        <text></text>
                    }
                }
            ]);

            self.addParam = function () {
                self.Param.push({ id: "1", name: "", ParamAll: self.ParamAll, selectedValue: '-1', disabled: '0' });
            };

            self.removeParam = function () {
                self.Param.remove(this);
            };
        }

        ko.applyBindings(new WorkKindViewModel());
        $("select[name^='ParamList']").trigger("change");

        $(document).ready(function () {
            @if (Model.ReadOnly)
            {
                <text>ReadOnlyView();</text>
            }
        });
    </script>
   
}